package com.cloud.bc.controller;

import cn.hutool.core.convert.Convert;
import com.cloud.bc.model.entity.BcArticles;
import com.cloud.bc.service.BcArticleCategorysService;
import com.cloud.bc.service.BcArticlesService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@AllArgsConstructor
@Controller
@RequestMapping("m/article")
public class ArticleController extends BaseController {

    private final BcArticleCategorysService bcArticleCategorysService;
    private final BcArticlesService bcArticlesService;

    /**
     * 文章
     */
    @GetMapping("")
    public ModelAndView article(HttpServletRequest request, HttpServletResponse response) {
        int cid = Convert.toInt(request.getParameter("cid"), 0);
        ModelAndView view = baseView(request,"文章", "m/article/index");
        view.addObject("categoryList", bcArticleCategorysService.selectAll(null));
        view.addObject("cid", cid);
        return view;
    }

    /**
     * 文章详情
     */
    @GetMapping("info/{id}")
    public ModelAndView articleInfo(HttpServletRequest request, HttpServletResponse response,
                                    @PathVariable("id") Integer id) {
        BcArticles byId = bcArticlesService.getById(id);
        ModelAndView view = baseView(request,byId.getTitle(), "m/article/info");
        view.addObject("data", byId);
        return view;
    }
}
